package utils

import (
	"bytes"
	"crypto/cipher"
	"crypto/des"
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/base64"
	"encoding/pem"
	"errors"
	"fmt"
	"golang.org/x/text/encoding/simplifiedchinese"
	"golang.org/x/text/transform"
	"io/ioutil"
)

const (
	base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
)

func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
	padding := blockSize - len(ciphertext)%blockSize
	padtext := bytes.Repeat([]byte{byte(padding)}, padding)
	return append(ciphertext, padtext...)
}

func DesEncrypt(origData, key []byte) ([]byte, error) {
	block, err := des.NewCipher(key)
	if err != nil {
		return nil, err
	}
	origData = PKCS5Padding(origData, block.BlockSize())
	blockMode := cipher.NewCBCEncrypter(block, key)
	crypted := make([]byte, len(origData))
	blockMode.CryptBlocks(crypted, origData)
	return crypted, nil
}

func DesDecrypt(crypted, key []byte) ([]byte, error) {
	block, err := des.NewCipher(key)
	if err != nil {
		return nil, err
	}
	blockMode := cipher.NewCBCDecrypter(block, key)
	origData := make([]byte, len(crypted))
	// origData := crypted
	blockMode.CryptBlocks(origData, crypted)
	origData = PKCS5UnPadding(origData)
	// origData = ZeroUnPadding(origData)
	return origData, nil
}

func PKCS5UnPadding(origData []byte) []byte {
	length := len(origData)
	// 去掉最后一个字节 unpadding 次
	unpadding := int(origData[length-1])
	return origData[:(length - unpadding)]
}

func Id5Encode(source string) (string, error) {
	if data, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader([]byte(source)), simplifiedchinese.GBK.NewEncoder())); err == nil {
		if dst, err := DesEncrypt(data, []byte("12345678")); err == nil {
			coder := base64.NewEncoding(base64Table)
			return coder.EncodeToString(dst), nil
		} else {
			return "", err
		}
	} else {
		return "", err
	}

}

func Id5Decode(source string) (string, error) {
	coder := base64.NewEncoding(base64Table)
	if data, err := coder.DecodeString(source); err == nil {
		if dst, err := DesDecrypt(data, []byte("12345678")); err == nil {
			if da, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader(dst), simplifiedchinese.GBK.NewDecoder())); err == nil {
				return string(da), nil
			} else {
				return "", err
			}
		} else {
			return "", err
		}
	} else {
		return "", err
	}

}
func RsaEncrypt(origData, publicKey []byte) ([]byte, error) {

	block, key := pem.Decode(publicKey)
	fmt.Println("key:=", string(key))
	if block == nil {

		return nil, errors.New("public key error")

	}

	pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)

	if err != nil {

		return nil, err

	}

	pub := pubInterface.(*rsa.PublicKey)

	return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)

}
func RsaDecrypt(ciphertext, privateKey []byte) ([]byte, error) {

	block, _ := pem.Decode(privateKey)

	if block == nil {

		return nil, errors.New("private key error!")

	}

	priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)

	if err != nil {
		return nil, err

	}
	return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
}
